package com.eyeem.recyclerviewtools.sample;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.ButtonBarLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.eyeem.recyclerviewtools.OnItemClickListener;
import com.eyeem.recyclerviewtools.sample.adapter.FragmentAdapter;
import com.eyeem.recyclerviewtools.sample.data.Photo;
import com.eyeem.recyclerviewtools.sample.fragment.ViewPagerFragment;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by budius on 26.05.16.
 */
public class Sample1Activity extends BaseActivity implements
        OnItemClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Nullable
    @Bind(R.id.refresh)
    SwipeRefreshLayout refresh;
    @Nullable
    @Bind(R.id.header_image)
    ImageView header;
    @Bind(R.id.viewpager)
    ViewPager viewpager;

    CollapsingToolbarLayoutState state = CollapsingToolbarLayoutState.EXPANDED;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Config config = (Config) getIntent().getSerializableExtra(Config.TAG);

        setContentView(R.layout.activity_sample_1);
        //if (config.userHeaderImage) {
        //请在layout根节点设置：android:fitsSystemWindows="true"
        // inflate and bind
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        // getWindow().setStatusBarColor(Color.TRANSPARENT);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        //} else {
        //    getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
        // }

        ButterKnife.bind(this);
        // Toolbar
        toolbar.setTitle("Hello Droids!");
        toolbar.inflateMenu(R.menu.menu);
        toolbar.setNavigationIcon(R.mipmap.back);
        toolbar.setCollapsible(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //toolbar.setLogo(R.drawable.header);
        // SwipeRefreshLayout
        if (refresh != null && header != null) {
            Picasso.with(this)
                    .load(Photo.HEADER(getResources().getDisplayMetrics().widthPixels))
                    .fit().centerCrop().into(header);

            dismissRefresh(refresh);
        }
        //if (config.useCoordinatorLayout) {
        final ButtonBarLayout playButton = (ButtonBarLayout) findViewById(R.id.playButton);
        final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbarLayout);
        final AppBarLayout appbar = (AppBarLayout) findViewById(R.id.appbar);
        //以下AppBarLayout代码参考http://www.jianshu.com/p/06c0ae8d9a96
        //AppBarLayout
//        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
//            @Override
//            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
//                if (verticalOffset == 0) {
//                    if (state != CollapsingToolbarLayoutState.EXPANDED) {
//                        state = CollapsingToolbarLayoutState.EXPANDED;//修改状态标记为展开
//                        collapsingToolbarLayout.setTitle("EXPANDED");//设置title为EXPANDED
//                    }
//                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
//                    if (state != CollapsingToolbarLayoutState.COLLAPSED) {
//                        collapsingToolbarLayout.setTitle("");//设置title不显示
//                        playButton.setVisibility(View.VISIBLE);//隐藏播放按钮
//                        state = CollapsingToolbarLayoutState.COLLAPSED;//修改状态标记为折叠
//                    }
//                } else {
//                    if (state != CollapsingToolbarLayoutState.INTERNEDIATE) {
//                        if (state == CollapsingToolbarLayoutState.COLLAPSED) {
//                            playButton.setVisibility(View.GONE);//由折叠变为中间状态时隐藏播放按钮
//                        }
//                        collapsingToolbarLayout.setTitle("INTERNEDIATE");//设置title为INTERNEDIATE
//                        state = CollapsingToolbarLayoutState.INTERNEDIATE;//修改状态标记为中间
//                    }
//                }
//            }
//        });
        collapsingToolbarLayout.setExpandedTitleGravity(Gravity.CENTER);
        collapsingToolbarLayout.setCollapsedTitleGravity(Gravity.CENTER);
        collapsingToolbarLayout.setExpandedTitleMargin(0,0,0,0);
        collapsingToolbarLayout.setScrimsShown(true);
        //}

        //ViewPager
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 0);
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 1);
        viewpager.setAdapter(adapter);
    }


    @Override
    public void onItemClick(RecyclerView parent, View view, int position, long id, RecyclerView.ViewHolder viewHolder) {
        popView(view, position);
    }

    private void popView(View view, int position) {
        fancyAnimation(view);
        toast("Clicked position " + position);
    }


    public enum CollapsingToolbarLayoutState {
        EXPANDED, COLLAPSED, INTERNEDIATE;
    }


}
